
package arbre;

import java.util.Collection;
import java.util.Iterator;
import java.util.Random;

class ArbreDev implements Comparable<ArbreDev>
{
	private Integer id = 0;
	private String nom = "david";

	public ArbreDev(int id, String nom)
	{
		this.id = id;
		this.nom = nom;
	}

	@Override
	public int compareTo(ArbreDev o)
	{
		return this.id.compareTo(o.id);
	}

	@Override
	public boolean equals(Object o)
	{
		ArbreDev ad = null;
		boolean ok = o instanceof ArbreDev;

		if (ok)
		{
			ad = (ArbreDev) o;
			ok = ad.id == this.id;
		}

		return ok;
	}

	@Override
	public String toString()
	{
		return this.id + " " + this.nom;
	}

	public static void main(String[] args)
	{
		test2();
	}

	public static void test2()
	{
		final int nbRand = 100;
		final int pop = 30;
		Random rand = new Random();
		Collection<ArbreDev> liste = null;
		Iterator<ArbreDev> ite = null;
		ArbreDev comp = null;
		ArbreBinaire<ArbreDev> ab = new ArbreBinaire<ArbreDev>();
		ArbreDev ad = null;
		int temp = 0;

		ad = new ArbreDev(20, "David");
		ab.add(ad);

		ad = new ArbreDev(13, "Nic");
		ab.set(ad);

		ad = new ArbreDev(25, "Lepage");
		ab.set(ad);

		ad = new ArbreDev(2, "Max");
		ab.add(ad);

		ad = new ArbreDev(23, "Goulet");
		ab.set(ad);

		ad = new ArbreDev(4, "Flo");
		ab.set(ad);

		ad = new ArbreDev(21, "Taaral");
		ab.add(ad);

		ad = new ArbreDev(1, "Will");
		ab.add(ad);

		ad = new ArbreDev(40, "Antoine");
		ab.set(ad);

		ad = new ArbreDev(23, "Phil");
		ab.set(ad);

		ad = new ArbreDev(26, "Phil2");
		ab.set(ad);

		liste = ab.values();
		ite = liste.iterator();

		while (ite.hasNext())
		{
			System.out.println(ite.next().toString());
		}
		ab.printValues();

		int key = 0;

		key = 26;
		ad = new ArbreDev(key, "Antoine");
		System.out.println();
		System.out.println("REMOVE " + key);
		ab.removeElement(ad);
		liste = ab.values();
		ite = liste.iterator();
		while (ite.hasNext())
		{
			System.out.println(ite.next().toString());
		}
		ab.printValues();

		key = 13;
		ad = new ArbreDev(key, "Antoine");
		System.out.println();
		System.out.println("REMOVE " + key);
		ab.removeElement(ad);
		liste = ab.values();
		ite = liste.iterator();
		while (ite.hasNext())
		{
			System.out.println(ite.next().toString());
		}
		ab.printValues();
	}

	public static void test1()
	{
		final int nbRand = 100;
		final int pop = 30;
		Random rand = new Random();
		Collection<ArbreDev> liste = null;
		Iterator<ArbreDev> ite = null;
		ArbreDev comp = null;
		ArbreBinaire<ArbreDev> ab = new ArbreBinaire<ArbreDev>();
		ArbreDev ad = null;
		int temp = 0;

		ad = new ArbreDev(20, "David");
		ab.add(ad);

		ad = new ArbreDev(13, "Nic");
		ab.set(ad);

		ad = new ArbreDev(25, "Lepage");
		ab.set(ad);

		ad = new ArbreDev(2, "Max");
		ab.add(ad);

		ad = new ArbreDev(23, "Goulet");
		ab.set(ad);

		ad = new ArbreDev(4, "Flo");
		ab.set(ad);

		ad = new ArbreDev(21, "Taaral");
		ab.add(ad);

		ad = new ArbreDev(1, "Will");
		ab.add(ad);

		ad = new ArbreDev(40, "Antoine");
		ab.set(ad);

		ad = new ArbreDev(23, "Phil");
		ab.set(ad);

		ad = new ArbreDev(26, "Phil2");
		ab.set(ad);

		for (int i = 0; i < pop; i++)
		{
			temp = rand.nextInt(nbRand);
			ad = new ArbreDev(temp, "TOTO_" + temp);
			ab.set(ad);
		}

		liste = ab.values();
		ite = liste.iterator();

		while (ite.hasNext())
		{
			System.out.println(ite.next().toString());
		}

		System.out.println("NB NODE " + ab.size());

		System.out.println("test gets");

		comp = ab.get(new ArbreDev(1, null));
		System.out.println("Get = " + comp);

		comp = ab.get(new ArbreDev(20, null));
		System.out.println("Get = " + comp);

		comp = ab.get(new ArbreDev(21, null));
		System.out.println("Get = " + comp);

		comp = ab.get(new ArbreDev(22, null));
		System.out.println("Get = " + comp);

		// ab.printValues();

		System.out.println("DEGREE " + ab.degree());

		System.out.println();
		ab.printValues();

		ab.debalancer();
		System.out.println();
		System.out.println("DEBALANCEMENT");
		ab.printValues();
		System.out.println("DEGREE " + ab.degree() + " nbNode " + ab.size());

		ab.rebalancer();
		System.out.println();
		System.out.println("REBALANCEMENT");
		ab.printValues();
		System.out.println("DEGREE " + ab.degree() + " nbNode " + ab.size());

		comp = ab.get(new ArbreDev(2, null));
		ab.removeElement(comp);
		System.out.println("Remove");
		ab.printValues();
	}
}
